package aom.ju.ss.utils.provinceCityDistrict

import android.app.Activity
import aom.ju.ss.R
import aom.ju.ss.databinding.PopCitypickerBinding
import aom.ju.ss.view.dialog.BaseDialog

class CustomCityPickerDialog(
    activity: Activity, configa: MutableList<CustomCityData>,
    var listener: OnCustomCityPickerItemClickListener
) :
    BaseDialog(activity), OnWheelChangedListener {

    private var binding: PopCitypickerBinding

    companion object {
        var dataList: MutableList<CustomCityData> = arrayListOf()
    }

    init {
        dataList = configa
        setWidth(-1)
        binding = vBinding as PopCitypickerBinding

        binding.tvCancel.setOnClickListener { dis() }
        setUpData(dataList)
        //确认选择
        binding.tvConfirm.setOnClickListener {
//            if (type == WheelType.PRO) {
//                val pCurrent: Int = binding.idProvince.currentItem
//                val provinceList: List<CustomCityData> = dataList
//                val province = provinceList[pCurrent]
//                listener.onSelected(province, CustomCityData(), CustomCityData())
//            } else if (type == WheelType.PRO_CITY) {
//                val pCurrent: Int = binding.idProvince.currentItem
//                val provinceList: List<CustomCityData> = dataList
//                val province = provinceList[pCurrent]
//                val cCurrent: Int = binding.idCity.currentItem
//                val cityDataList = province.list
//                val city = cityDataList[cCurrent]
//                listener.onSelected(province, city, CustomCityData())
//            } else if (type == WheelType.PRO_CITY_DIS) {
                val pCurrent: Int = binding.idProvince.currentItem
                val provinceList: List<CustomCityData> = dataList
                val province = provinceList[pCurrent]
                val cCurrent: Int = binding.idCity.currentItem
                val cityDataList = province.list
                val city = cityDataList[cCurrent]
                val dCurrent: Int = binding.idDistrict.currentItem
                val areaList = city.list
                val area = areaList[dCurrent]
                listener.onSelected(province, city, area)
//            }
            dis()
        }


    }

    fun setUpData(config: MutableList<CustomCityData>) {
        val proArra: List<CustomCityData> = config
        val arrayWheelAdapter: ArrayWheelAdapter<*> = ArrayWheelAdapter(activity, proArra)
        arrayWheelAdapter.itemResource = R.layout.default_item_city
        arrayWheelAdapter.itemTextResource = R.id.default_item_city_name_tv
        binding.idProvince.viewAdapter = arrayWheelAdapter



        binding.idProvince.addChangingListener(this)
        binding.idCity.addChangingListener(this)
        binding.idDistrict.addChangingListener(this)


        binding.idProvince.lineColorStr

        updateCities()
    }

    /**
     * 根据当前的省，更新市WheelView的信息
     */
    fun updateCities() {
        //省份滚轮滑动的当前位置
        val pCurrent: Int = binding.idProvince.currentItem

        //省份选中的名称
        val proArra: List<CustomCityData> = dataList
        if (proArra.isEmpty() || proArra.size == pCurrent) {
            return
        }
        val mProvinceBean: CustomCityData = try {
            proArra[pCurrent]
        } catch (e: Exception) {
            return
        }
        val pCityList = mProvinceBean.list
        val cityWheel: ArrayWheelAdapter<*> = ArrayWheelAdapter(activity, pCityList)
        cityWheel.itemResource = R.layout.default_item_city
        cityWheel.itemTextResource = R.id.default_item_city_name_tv
        binding.idCity.viewAdapter = cityWheel
        updateAreas()
    }

    /**
     * 根据当前的市，更新区WheelView的信息
     */
    fun updateAreas() {
        val pCurrent: Int = binding.idProvince.currentItem
        val cCurrent: Int = binding.idCity.currentItem
        val province = dataList[pCurrent]
        val cityDataList = province.list
        if (cityDataList == null || cityDataList.size <= cCurrent) return
        val city = cityDataList[cCurrent]
        val areaList = city.list
        val districtWheel: ArrayWheelAdapter<*> = ArrayWheelAdapter(activity, areaList)
        districtWheel.itemResource = R.layout.default_item_city
        districtWheel.itemTextResource = R.id.default_item_city_name_tv
        binding.idDistrict.viewAdapter = districtWheel
        binding.idDistrict.currentItem = 0
    }

    override fun isBottom(): Boolean {
        return true
    }

    override fun getLay(): Int {
        return R.layout.pop_citypicker
    }

    override fun onChanged(wheel: WheelView?, oldValue: Int, newValue: Int) {
        if (wheel === binding.idProvince) {
            updateCities()
        } else if (wheel === binding.idCity) {
            updateAreas()
        } else if (wheel === binding.idProvince) {
        }

    }

}